package com.etc.esproject.controller;

import com.etc.esproject.dao.EmployeeRepository;
import com.etc.esproject.entity.Employee;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 上等的猪头肉
 * @Title: ElasticSearchController
 * @ProjectName es-project
 * @Description: TODO
 * @date 2018/9/28 14:38
 */
@RestController
public class ElasticSearchController {

    @Autowired
    private EmployeeRepository er;

    //增加
    @GetMapping("add")
    public String add() {

        Employee employee = new Employee();
        employee.setId("1");
        employee.setFirstName("sdfsa");
        employee.setLastName("safda");
        employee.setAge(26);
        employee.setAbout("sadfsa");
        er.save(employee);
        return "success";
    }

    //局部更新
    @RequestMapping("/update")
    public String update() {

        Employee employee = er.queryEmployeeById("1");
        employee.setFirstName("哈哈");
        er.save(employee);

        System.err.println("update a obj");

        return "success";
    }

    //删除
    @RequestMapping("/delete")
    public String delete() {
        Employee employee = new Employee();
        employee.setId("1");
        er.delete(employee);

        return "success";
    }


    //查询
    @RequestMapping("/query/{id}")
    public Employee query(@PathVariable("id") String id) {

        Employee accountInfo = er.queryEmployeeById(id);
        System.err.println(new Gson().toJson(accountInfo));

        return accountInfo;
    }

}
